<?php

namespace backend\models;

use Yii;
use common\models\UserInfo;
use yii\helpers\ArrayHelper;
use yii\data\ActiveDataProvider;
use yii\db\ActiveRecord;
use backend\models\CrowdItem;

class CrowdItemSearch extends CrowdItem
{
    /**
     * @inheritdoc
     */
 
    /**
     * @inheritdoc
     */
    public function rules()
    {    	
        return [
            [['id'], 'integer'],
            [['state','banner_flag','useing'], 'safe'],
        ];
    }
    public function search($params){
    	$query = CrowdItem::find()->joinWith("item")->joinWith('image')->joinwith("item.defaultThumbImage")->select("sys_image.url  crowd_url,mainItemImgM.url item_url,crowd_item.*, sys_item.name item ")->orderBy("id desc");
    	/* $sql=$query->createCommand()->getRawSql();
    	 return $sql; */
    	$dataProvider = new ActiveDataProvider([
    			'query' => $query,
    			'pagination'=>['pageSize' => '10'],
    			'sort'=>false
    	]);
    	$this->load($params);
    	if (!$this->validate()) {
    		return $dataProvider;
    	}
    	$query->andFilterWhere([
    			'crowd_item.id' => $this->id,
    	        'crowd_item.state'=>$this->state,
        	    'crowd_item.banner_flag'=>$this->banner_flag,
        	    'crowd_item.useing'=>$this->useing,
    	    
    	]);
    	$query->andFilterWhere(['desc', 'desc', $this->desc])
    	;
    	return $dataProvider;
    	
    }
    /**
     * 众筹失败列表
     */
    public function crwodrefundsearch($params){
        $query = CrowdItem::find()->joinWith("item")->joinWith('image')->joinwith("item.defaultThumbImage")
        ->select("sys_image.url  crowd_url,mainItemImgM.url item_url,crowd_item.*, sys_item.name item ")
        ->where("state=3")
        ->orderBy("id desc");
        /* $sql=$query->createCommand()->getRawSql();
         return $sql; */
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination'=>['pageSize' => '10'],
            'sort'=>false
        ]);
        $this->load($params);
        if (!$this->validate()) {
            return $dataProvider;
        }
        $query->andFilterWhere([
            'crowd_item.id' => $this->id,
            'crowd_item.state'=>$this->state,
            'crowd_item.banner_flag'=>$this->banner_flag,
            'crowd_item.useing'=>$this->useing,
        ]);
        $query->andFilterWhere(['desc', 'desc', $this->desc])
        ;
        return $dataProvider;
        
    }
    /**
     * 众筹已退款订单列表
     * @param   $params
     */
    public function logrefundsearch($params){
        $query=PayTrade::find()->joinWith("userInfo") 
        ->select(" pay_trade.*,user_info.username,user_info.tel")
        ->where(" pay_trade.trade_class=2 and  price_state=3  and  trade_state=5");
        $dataProvider=new ActiveDataProvider([
            'query'=>$query,
            'pagination'=>['pageSize'=>'10'],
            'sort' => ['defaultOrder' => ['id'=>SORT_DESC]]
        ]);
        
        $this->load($params);
        if(!$this->validate()){
            return $dataProvider;
        }
        $query->andFilterWhere([
            'user_info.nickname'=>$this->nickname,
            'user_info.username'=>$this->username,
            'user_info.tel'=>$this->tel,
            'pay_trade.id'=>$this->id
        ]);
        return $dataProvider;
    }
   
    /**
     * 退款查询
     * @param unknown $params
     */
    public function refundsearch($params,$crowd_id){
       /*  $sql="SELECT  pay_trade.*,user_info.username,user_info.tel   FROM pay_trade
               left join user_info on user_info.id=pay_trade.user_id
                 WHERE price_state=1 and crowd_price_id IN  
            (SELECT   crowd_price.id crowd_price_id  FROM crowd_item
              LEFT JOIN crowd_price  ON crowd_price.`crowd_id`=crowd_item.`id`
                 WHERE crowd_item.`state`=3)
              and  trade_state!=5  "; */
       // $query=PayTrade::findBySql($sql)->orderBy("pay_trade.id desc ");
       $query=PayTrade::find()->joinWith("userInfo")->joinWith("crowdPrice.crowdItem")
                    ->select("crowd_item.desc,pay_trade.*,user_info.username,user_info.tel")
                ->where("crowd_price_id IN  
                            (SELECT   crowd_price.id crowd_price_id  FROM crowd_item
                             LEFT JOIN crowd_price  ON crowd_price.`crowd_id`=crowd_item.`id`
                             WHERE crowd_item.id={$crowd_id} and  crowd_item.`state`=3)
                     and  price_state=1  and  trade_state=1");
        $dataProvider=new ActiveDataProvider([
            'query'=>$query,
            'pagination'=>['pageSize'=>'10'],
            'sort' => ['defaultOrder' => ['id'=>SORT_DESC]]
        ]);
        
        $this->load($params);
        if(!$this->validate()){
            return $dataProvider;
        }
        $query->andFilterWhere([
            'user_info.nickname'=>$this->nickname,
            'user_info.username'=>$this->username,
            'user_info.tel'=>$this->tel,
            'pay_trade.id'=>$this->id
        ]);
        return $dataProvider;
    }
    
}
